//
//  MvViewController.m
//  Coco_Music
//
//  Created by K。 on 15/6/20.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "MvViewController.h"

@interface MvViewController ()
@property (strong, nonatomic)UIView *bigView;
@end

@implementation MvViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray array];
        
    }
    return self;
}

- (void)loadView
{
    self.bigView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.bigView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
    self.tView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64-44) style:UITableViewStylePlain];
    _tView.delegate = self;
    _tView.dataSource = self;
    _tView.backgroundColor = [UIColor whiteColor];
    _tView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:_tView];
    self.pageNum = 1;
    [self getData];
    self.tView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载,请稍后";
    [self.HUD show:YES];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"R96";
    MVTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[MVTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:str];
    }
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
       
    }];
    cell.mv =[_dataArr objectAtIndex:indexPath.row];
    
    
    return cell;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(void)getData
{
    
    if (self.tView.footer.isRefreshing == YES && self.pageNum <= self.allPage) {
        self.pageNum++;
        NSLog(@"%ld",self.pageNum);
    }
    NSLog(@"下拉刷新了");
NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/channel/channel/mvs?page=%ld&size=10&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone2C4&f=f320&s=s330&imsi=&hid=&splus=8.3&active=1&net=2&openudid=5df9002635a21cc5f7f660e71a16d1e35e5a04a0&idfa=B5F4461B-F9C7-4F1E-BA92-EB2E683311DF&utdid=VYEt7JjHCIUDAGowp1W3cSW3&alf=201200&bundle_id=com.ttpod.music",self.pageNum];
    
    [AFN_block NSString:str Blokc:^(id request) {
         NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:request];
        
        NSMutableArray *DataArray =[dic objectForKey:@"data"];
        self.allPage = [[dic objectForKey:@"pageCount"]integerValue];
        for (NSMutableDictionary *myDic in DataArray) {
             MV *mv =[[MV alloc]init];
            mv.MVid =[[myDic objectForKey:@"id"]integerValue];
            mv.title =[myDic objectForKey:@"title"];
            mv.desc =[myDic objectForKey:@"desc"];
            mv.tag=[myDic objectForKey:@"tag"];
            mv.mvList =[myDic objectForKey:@"mvList"];
            mv.bigPicUrl =[myDic objectForKey:@"bigPicUrl"];
            for (NSMutableDictionary *tagDic in mv.tag) {
                mv.tagName =[tagDic objectForKey:@"tagName"];
                mv.tagColor =[tagDic objectForKey:@"tagColor"];
                NSLog(@"gaagag ahahah%ld %@",mv.MVid,str);
                
            }
            for (NSMutableDictionary *mvListDic in mv.mvList) {
                mv.MvListid =[[mvListDic objectForKey:@"id"]integerValue];
                mv.path =[mvListDic objectForKey:@"path"];
                mv.url =[mvListDic objectForKey:@"url"];
                
            }
            
            [self.dataArr addObject:mv];
            [self.HUD hide:YES];
            [self.tView.footer endRefreshing];
            [mv release];
        }
        
        [self.tView reloadData];
           }];

}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
  [self.myTable deselectRowAtIndexPath:indexPath animated:YES];
    MVPlayerViewController *mvVC = [[MVPlayerViewController alloc] init];
    mvVC.myMV =[self.dataArr objectAtIndex:indexPath.row];
    mvVC.MvID =[[self.dataArr objectAtIndex:indexPath.row]MVid];
    [self.navigationController pushViewController:mvVC animated:YES];

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIScreen mainScreen].bounds.size.width/3*2;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
